package cn.coastalinfo.project.drools.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.coastalinfo.common.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.coastalinfo.common.utils.StringUtils;
    import cn.coastalinfo.framework.web.page.PageQuery;
    import cn.coastalinfo.framework.web.page.TableDataInfo;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.coastalinfo.project.drools.domain.bo.RuleBo;
import cn.coastalinfo.project.drools.domain.vo.RuleVo;
import cn.coastalinfo.project.drools.domain.Rule;
import cn.coastalinfo.project.drools.mapper.RuleMapper;
import cn.coastalinfo.project.drools.service.IRuleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 规则Service业务层处理
 *
 * @author hadge
 * @date 2024-06-07
 */
@RequiredArgsConstructor
@Service
public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements IRuleService {

    @Autowired
    private RuleMapper baseMapper;

    /**
     * 查询规则
     */
    @Override
    public RuleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

        /**
         * 查询规则列表
         */
        @Override
        public TableDataInfo<RuleVo> queryPageList(RuleBo bo, PageQuery pageQuery) {
            LambdaQueryWrapper<Rule> lqw = buildQueryWrapper(bo);
            Page<RuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
            return TableDataInfo.build(result);
        }

    /**
     * 查询规则列表
     */
    @Override
    public List<RuleVo> queryList(RuleBo bo) {
        LambdaQueryWrapper<Rule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<RuleVo> getVailList(RuleBo bo) {
        LambdaQueryWrapper<Rule> lqw = Wrappers.lambdaQuery();
        lqw.isNotNull(Rule::getContent);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Rule> buildQueryWrapper(RuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Rule> lqw = Wrappers.lambdaQuery();
                    lqw.like(StringUtils.isNotBlank(bo.getName()), Rule::getName, bo.getName());
                    lqw.eq(StringUtils.isNotBlank(bo.getContent()), Rule::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增规则
     */
    @Override
    public Long insertByBo(RuleBo bo) {
        Rule add = BeanUtil.toBean(bo, Rule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add.getId();
    }

    /**
     * 修改规则
     */
    @Override
    public Long updateByBo(RuleBo bo) {
        Rule update = BeanUtil.toBean(bo, Rule.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return update.getId();
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Rule entity){
        long id = ObjectUtil.isNull(entity.getId()) ? -1L : entity.getId();
        LambdaQueryWrapper<Rule> lqw = Wrappers.lambdaQuery();
        Rule info = baseMapper.selectOne(new LambdaQueryWrapper<Rule>()
                .select(Rule::getId, Rule::getName,Rule::getTitle)
//                .eq(Rule::getName, entity.getName())
//                .or()
                .eq(Rule::getTitle, entity.getTitle()));
        if (ObjectUtil.isNotNull(info) && info.getId() != id) {
            if(info.getTitle().equals(entity.getTitle()) ){
                throw new ServiceException("规则名称不能重复");
            }
            throw new ServiceException("规则名称不能重复");
        }
    }

    /**
     * 批量删除规则
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    public int checkRuleNameUnique(RuleBo rule) {
        return 0;
    }
}
